package mail;

import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.CustomScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import lecture.Lecture;
import lecture.LectureManagedBean;

/**
 *
 * @author Thorsten Ludwig
 */

@ManagedBean(name="showMailsBean")
@CustomScoped(value="#{window}")
public class ShowMailsManagedBean {
    
    private List<MailContainer> mails;
    
    private Lecture lecture;
    
    public ShowMailsManagedBean() {
        FacesContext context = FacesContext.getCurrentInstance();
        LectureManagedBean lb = context.getApplication().evaluateExpressionGet(context, "#{lectureBean}", LectureManagedBean.class);
        this.lecture = lb.getLecture();
        this.mails = new ArrayList<MailContainer>();
        this.initMails();
    }

    private void initMails() {
        List<Mail> list = lecture.getMails();
        for (Mail m: list) {
            this.mails.add(new MailContainer(m));
        }
    }
    
    public List<MailContainer> getMails() {
        if (this.lecture.getMails().size() != this.mails.size()) {
            this.initMails();
        }
        return this.mails;
    }
    
}
